//JavaScript
var currentMilliSecond;
var firstOver;
var mouseMove;
var timeout;

function getLinkImage(illustration, ID){
//CREATION DU SQUELETE HTML
var html = ""+
	 "<A ID='"+ID+"' class='blanc thickbox' "+
                "HREF='illustrations/"+illustration.url+"'"+
                " onMouseOver='javascript:doAClick("+ID+");'"+
                " onMouseOut='javascript:resetFirstOver();'"+
                " onMouseMove='javascript:setMouseMove();'"+
                ">"+
    "<IMG WIDTH='200px' SRC='illustrations/"+illustration.url+"'><BR/>";
    if (illustration.legende != ""){
		    html += html_entity_decode(illustration.legende);
		}
		html +="</A>";

return html;
}

function initTB(){
tb_init('a.thickbox, area.thickbox, input.thickbox');
}

function doAClick(link){
  if (canDo()){
    setTimeout(function(){
      if (!firstOver){
        // Obligatoire pour que le thickBox marche
        $("#"+link).click();
        
        
        $("#TB_overlay").bind('mousemove', setMouseMove);
        $("#TB_window").bind('mousemove', setMouseMove);
        $("#"+link).bind('mousemove', setMouseMove);
        timeout = false;
        setMouseMove();
        checkClose();
      }
    }
    ,2000);
    
  }
}


function canDo(){
  var result = false;
  var secondTemp = new Date().getTime();  
  if (((currentMilliSecond != null) && ((secondTemp - currentMilliSecond) > 500))
    || (currentMilliSecond == null)){
    currentMilliSecond = secondTemp;
    result = true;
    firstOver = false;
  }
  return result;
}

function setMouseMove(){
  mouseMove = true;
  if (!timeout){
    timeout = true;
    setTimeout(function(){
      timeout = false;
      mouseMove = false;
    }
    ,3000);
  }
}



function resetFirstOver(){
  firstOver = true;
}

function closeTB(){
  if (canDo()){
    tb_remove();
  }
}

function checkClose(){
  if (!mouseMove){
    closeTB();
  }else{
    setTimeout(checkClose,2000);
  }
}
